﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Edit_IQLastTimeRemain : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public int id;

        public frm_Edit_IQLastTimeRemain()
        {
            InitializeComponent();
            this.id = 0;
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.inputDate.EditValue = DateTime.Now.Date;
        }

        ///////////////////////////////////
        /// Load Data
        ///////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (id == 0)
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [241_IQLastTimeRemain]"
                        + " WHERE ID=" + id;                        
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.inputDate.EditValue = DateTime.Parse(dataReader["LastDate"].ToString());
                        this.iqID.Text = dataReader["IQID"].ToString();
                        this.remainQuantity.Text = dataReader["Remain"].ToString();
                    }
                    dataReader.Close();
                    myConnection.Close();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    ret = false;
                }
            }

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            DateTime inputdate = new DateTime();            
            int remain = 0;

            if (this.inputDate.EditValue == null)
            {
                error = "Please select input Date";
            }
            else
            {
                inputdate = DateTime.Parse(this.inputDate.EditValue.ToString());
            }           

            if (string.IsNullOrEmpty(error))
            {
                if (!Int32.TryParse(this.remainQuantity.Text, out remain))
                {
                    error = "Invalid Remain Quantity";
                }
                else
                {
                    if (remain < 0)
                    {
                        error = "Remain quantity must be greater than 0.";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [241_IQLastTimeRemain] SET LastDate=@inputdate, Remain=" + remain
                            + "WHERE ID=" + id;
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@inputdate", SqlDbType.DateTime).Value = inputdate.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("IQ Last Time Remain added success!");
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot add IQ Last Time Remain. Please try again!");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }        
    }
}

